<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>页面控制台 - WingsJ</title>

  <style>
    html {
      font-size: 20px;
      font-family: 'Microsoft Jhenghei';
    }

    body {
      padding: 4rem;
    }
  </style>
  <script type="module">
    import Stats from './lib/stats.module.js';
    import { GUI } from './lib/dat.gui.module.js';

    window.Control = {}

    const AnimationArray = [ // 需要周期调用的函数添加到这个数组中
      () => {
        window.requestAnimationFrame(timestamp => {
          for (let el of AnimationArray) {
            el(timestamp)
          }
        })
      }
    ]
    window.Control.AnimationArray = AnimationArray

    const stats = new Stats()
    AnimationArray.push(() => {
      stats.update()
    })
    const gui = new GUI();
    window.Control.GUI = gui

    document.addEventListener('DOMContentLoaded', () => {
      document.body.appendChild(stats.dom)
      window.requestAnimationFrame(AnimationArray[0])
    })
  </script>

  <style>
    .div {
      width: 100px;
      height: 100px;
      margin-bottom: 2rem;
      background-color: skyblue;
    }

    .animation {
      animation: move 3s 1s;
    }

    #div-1 {
      animation-fill-mode: none;
    }

    #div-2 {
      animation-fill-mode: forwards;
    }

    #div-3 {
      animation-fill-mode: backwards;
    }

    #div-4 {
      animation-fill-mode: both;
    }

    @keyframes move {
      from {
        transform: translateX(0);
        background-color: orange;
      }

      to {
        transform: translateX(300px);
        background-color: pink;
      }
    }
  </style>
</head>

<body>
  <div>#1-none</div>
  <div id="div-1" class="div animation"></div>
  <div>#2-forwards</div>
  <div id="div-2" class="div animation"></div>
  <div>#3-backwards</div>
  <div id="div-3" class="div animation"></div>
  <div>#4-both</div>
  <div id="div-4" class="div animation"></div>

  <script type="module">
    let divs = Array.from(document.querySelectorAll('.div'))

    let param = {
      pause: () => {
        Array.from(document.querySelectorAll('.div')).forEach(el => el.style['animation-play-state'] = 'paused')
      },
      run: () => {
        Array.from(document.querySelectorAll('.div')).forEach(el => el.style['animation-play-state'] = 'running')
      },
      restart: () => {
        for (let el of divs) {
          el.classList.remove('animation');
          setTimeout(() => { el.classList.add('animation'); }, 0);
        }
      }
    }
    for (let key in param) {
      Control.GUI.add(param, key)
    }
  </script>
</body>

</html>